

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getWriter().println("get");
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		
		
		
		String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
		String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
		File tempFile = new File(tempPath);
		if(!tempFile.exists()) {
			tempFile.mkdir();
		}
		
		
		DiskFileItemFactory factory = new DiskFileItemFactory();
		factory.setSizeThreshold(1024 * 100);
		factory.setRepository(tempFile);
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setProgressListener(new ProgressListener() {
			
			@Override
			public void update(long pBytesRead, long pContentLength, int arg2) {
//				System.out.println("文件大小：" + pContentLength 
//						+ ",当前已处理： " + pBytesRead );
			}
		});
		//解决上传文件名的中文乱码
		upload.setHeaderEncoding("utf-8");
		if(!ServletFileUpload.isMultipartContent(req)) {
			return;
		}
		//
		//upload.setFileSizeMax(1024 * 1024);
		//		upload.setSizeMax(1024 * 1024);
		
		try {
			List<FileItem> list = upload.parseRequest(req);
			for(FileItem item : list) {
				if(item.isFormField()) {
					String name = item.getFieldName();
					String value = item.getString("utf-8");
					System.out.println(name + "=" + value);
				} else { //如果filtItem中封装的是文件
					String fileName = item.getName();
					if(fileName == null || 
							fileName.trim().equals("")) {
						continue;
					}
					fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);
					String fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1);
					InputStream is = item.getInputStream();
					String saveFilename = UUID.randomUUID().toString() + "." + fileExtName;
					File saveFile = new File(savePath, saveFilename);
					System.out.println(saveFile);
					FileOutputStream fos = new FileOutputStream(saveFile);
					byte[] buf = new byte[1024];
					int len = 0;
					while((len = is.read(buf)) > 0) {
						fos.write(buf, 0, len);
					}
					is.close();
					fos.close();
				}
				
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}
		
	}

	
	
}
